﻿<template>
  <van-popup v-model="visible" position="bottom" closeable>
    <van-form @submit="handleSubmit" :loading="loading">
      <van-field
        v-model="formFields.AppId"
        name="AppId"
        label="应用Id"
        :rules="[{ required: true, message: '请输入应用Id' }]"
      />
      <van-field
        v-model="formFields.AppSecret"
        name="AppSecret"
        label="密钥"
        :rules="[{ required: true, message: '请输入密钥' }]"
      />
      <van-field
        v-model="formFields.AppName"
        name="AppName"
        label="应用名"
        :rules="[{ required: true, message: '请输入应用名' }]"
      />

      <div style="margin: 16px;">
        <van-button
          block
          type="info"
          native-type="submit"
          v-if="hasPerm('Base_AppSecret.Edit')"
          >提交</van-button
        >
      </div>
    </van-form>
  </van-popup>
</template>

<script>
import {
  Base_AppSecret_GetTheData,
  Base_AppSecret_SaveData
} from "@/api/Base_Manage/Base_AppSecret";
import { mapGetters } from "vuex";

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      formFields: {
        AppId: "",
        AppSecret: "",
        AppName: ""
      },
      entity: {}
    };
  },
  computed: {
    ...mapGetters(["hasPerm"])
  },
  methods: {
    add() {
      this.entity = {};
      this.visible = true;
      this.formFields = {
        AppId: "",
        AppSecret: "",
        AppName: ""
      };
    },
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.formFields = {
          AppId: "",
          AppSecret: "",
          AppName: ""
        };

        Base_AppSecret_GetTheData({ id: id }).then(resJson => {
          this.entity = resJson.Data;
          Object.keys(this.formFields).forEach(item => {
            this.formFields[item] = this.entity[item];
          });
        });
      });
    },

    handleSubmit(value) {
      this.entity = Object.assign(this.entity, this.formFields);
      this.loading = true;
      Base_AppSecret_SaveData(this.entity).then(resJson => {
        this.loading = false;

        if (resJson.Success) {
          this.$toast("操作成功");
          this.afterSubmit();
          this.visible = false;
        } else {
          this.$toast(resJson.Msg);
        }
      });
    }
  }
};
</script>
